package com.timothy.android.handler;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;

import com.timothy.android.LoginActivity;
import com.timothy.android.util.ActivityStackControlUtil;
import com.timothy.android.util.SysUtil;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

/**
 * 自定义的 异常处理类 , 实现了 UncaughtExceptionHandler接口 
 * @author Administrator
 *
 */

@Deprecated
public class MyCrashHandler implements UncaughtExceptionHandler {
	private final static String LOG_TAG = "MyCrashHandler";
	
	// 需求是 整个应用程序 只有一个 MyCrash-Handler 
	private static MyCrashHandler myCrashHandler ;
	private Context context;
//	private DoubanService service;
//	private SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
	public static final int  EXIT_APPLICATION = 0x0001;  
	
	//1.私有化构造方法
	private MyCrashHandler(){
		
	}
	
	public static synchronized MyCrashHandler getInstance(){
		if(myCrashHandler!=null){
			return myCrashHandler;
		}else {
			myCrashHandler  = new MyCrashHandler();
			return myCrashHandler;
		}
	}
	public void init(Context context){
		this.context = context;
//		this.service = service;
	}
	

	public void uncaughtException(Thread arg0, Throwable arg1) {
		Log.i(LOG_TAG,"uncaughtException()...");
		
//		// 1.获取当前程序的版本号. 版本的id
//		String versioninfo = getVersionInfo();
//		Log.i(LOG_TAG,"Pversioninfo:"+versioninfo);
//		
//		// 2.获取手机的硬件信息.
//		String mobileInfo  = getMobileInfo();
//		Log.i(LOG_TAG,"mobileInfo:"+mobileInfo);
//		
//		// 3.把错误的堆栈信息 获取出来 
		String errorinfo = getErrorInfo(arg1);
		Log.i(LOG_TAG,"errorinfo:"+errorinfo);
		
//		new AlertDialog.Builder(context).setTitle("Warning").setMessage("System need to restart!")
//		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//			public void onClick(DialogInterface dialog, int which) {
//				ActivityStackControlUtil.finishProgram();
//			}
//		}).show();
		
//		Toast.makeText(context, "System need to restart",Toast.LENGTH_SHORT).show();
		
		ActivityStackControlUtil.finishProgram();
		
	}

	/**
	 * 获取错误的信息 
	 * @param arg1
	 * @return
	 */
	private String getErrorInfo(Throwable arg1) {
		Writer writer = new StringWriter();
		PrintWriter pw = new PrintWriter(writer);
		arg1.printStackTrace(pw);
		pw.close();
		String error= writer.toString();
		return error;
	}

	/**
	 * 获取手机的硬件信息 
	 * @return
	 */
	private String getMobileInfo() {
		StringBuffer sb = new StringBuffer();
		//通过反射获取系统的硬件信息 
		try {

			Field[] fields = Build.class.getDeclaredFields();
			for(Field field: fields){
				//暴力反射 ,获取私有的信息 
				field.setAccessible(true);
				String name = field.getName();
				String value = field.get(null).toString();
				sb.append(name+"="+value);
				sb.append("\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	/**
	 * 获取手机的版本信息
	 * @return
	 */
	private String getVersionInfo(){
		try {
			PackageManager pm = context.getPackageManager();
			 PackageInfo info =pm.getPackageInfo(context.getPackageName(), 0);
			 return  info.versionName;
		} catch (Exception e) {
			e.printStackTrace();
			return "版本号未知";
		}
	}
}
